package me.green.ui.system;

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import me.green.ui.LocalCache;
import me.green.ui.dialog.AboutDialog;
import me.green.ui.dialog.UnLockDialog;
import me.green.ui.global.UIResources;

/**
 * 系统托盘菜单
 *
 * @author green
 */
public class TrayMenu implements ActionListener {

    private static JFrame owner = null;

    private SystemTray platTray = null;
    private PopupMenu trayMenu = null;
    private TrayIcon logoIcon = null;
    private MenuItem itemLock = null;

    private String sLock = "Lock";
    private String sUnlock = "Unlock";

    private TrayMenu() {
        buildTrayMenu();
    }

    public static TrayMenu getInstances(JFrame owner) {
        TrayMenu.owner = owner;
        return new TrayMenu();
    }

    /**
     * 初始化托盘菜单
     */
    private void buildTrayMenu() {
        if (SystemTray.isSupported()) {
            platTray = SystemTray.getSystemTray();

            trayMenu = new PopupMenu();
            MenuItem itemOpen = new MenuItem("Open");
            itemOpen.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    actionOfShowWindowCheck();
                }
            });

            itemLock = new MenuItem(sLock);
            itemLock.addActionListener(this);
            itemLock.setActionCommand("lock");


            MenuItem itemAbout = new MenuItem("About");
            itemAbout.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    new AboutDialog(null, true);
                }
            });

            MenuItem itemExit = new MenuItem("Exit");
            itemExit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            trayMenu.add(itemOpen);
            trayMenu.add(itemAbout);
            trayMenu.add(itemLock);
            trayMenu.add(itemExit);

            logoIcon = new TrayIcon(new ImageIcon(getClass().getResource(UIResources.ICON_TRAY)).getImage(),
                    "电子手账本",
                    trayMenu);
            logoIcon.setImageAutoSize(true);

            logoIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getButton() == MouseEvent.BUTTON1) {
                        actionOfShowWindowCheck();
                    }
                }
            });
            try {
                platTray.add(logoIcon);
            } catch (AWTException e1) {
                e1.printStackTrace();
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("lock")) {
            MenuItem itemLock = (MenuItem) e.getSource();
            actionOfItemLock(itemLock);
        }
    }

    /**
     * Item of LockItem 's action
     *
     * @param itemLock
     */
    private void actionOfItemLock(MenuItem itemLock) {
        if (itemLock.getLabel().equals(sLock)) {
            itemLock.setLabel(sUnlock);
            LocalCache.setLock(true);
        } else {
            UnLockDialog.showInputPasswordDialog(this);
        }
    }

    /**
     * Item of Open Green Plan 's Action
     */
    public void actionOfShowWindowCheck() {
        if (itemLock.getLabel().equals(sLock)) {
            owner.setAlwaysOnTop(true);
            owner.setExtendedState(JFrame.NORMAL);
            owner.setVisible(true);
            owner.setAlwaysOnTop(false);
            platTray.remove(logoIcon);
        } else {
            int keyCode = JOptionPane.showConfirmDialog(null,
                    "已被锁定，需输入密码");
            if (keyCode == JOptionPane.OK_OPTION) {
                UnLockDialog.showInputPasswordDialog(TrayMenu.this);
                if (itemLock.getLabel().equals(sLock)) {
                    platTray.remove(logoIcon);
                }
            }
        }
    }

    /**
     * if window is showing, remove the icon from system tray
     */
    public void removeIcon() {
        if (platTray != null && logoIcon != null) {
            platTray.remove(logoIcon);
        }
    }

    /**
     * get the item of lock object
     *
     * @return
     */
    public MenuItem getItemLock() {
        return itemLock;
    }

    /**
     * lock action
     */
    public void lock() {
        if (itemLock != null) {
            itemLock.setLabel(sUnlock);
        }
    }

}
